package com.cheyun.netsalev3.view.defeatdetails

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.DefeatDetailsFragmentBinding
import com.cheyun.netsalev3.databinding.DetailsFragmentItemBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.viewmodel.ClueDetailsActivityViewModel

class DefeatDetailsFragment : BaseFragment<DefeatDetailsFragmentBinding>() {
    override fun initContentView(): Int {
        return R.layout.defeat_details_fragment
    }

    override fun initViewModel(): ViewModel? {
        activity?.apply {
            return ViewModelProviders.of(this).get(ClueDetailsActivityViewModel::class.java)
        }
        return null
    }

    override fun initView() {
        binding.messageBut.setOnClickListener {
            binding.phoneText.text.apply {
                FunctionUtils.sendSMSTo(context!!, binding.phoneText.text.toString(), "")
            }
        }
    }

    override fun initData() {
        // android:drawableLeft='@{viewModel.clueDetails.get().sex=="1"? @drawable/a01_1_zbsh_headboy:@drawable/a01_1_zbsh_headgirl}'
        binding.viewModel?.sex?.observe(this, Observer { it ->
            val drawableLeft = resources.getDrawable(if(it.equals("1")){R.drawable.a01_1_zbsh_headboy}else{R.drawable.a01_1_zbsh_headgirl})
            binding.txtUname.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null)
        })

//        binding.txtUname.setCompoundDrawablePadding(4)
        binding.viewModel?.bottomList?.observe(this, Observer { list ->
            binding.linearLayout.removeAllViews()
            for (i in list) {
                var itembinding =
                        DataBindingUtil.inflate<DetailsFragmentItemBinding>(LayoutInflater.from(context),
                                R.layout.details_fragment_item, null, false)
                var param = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
                itembinding.root.layoutParams = param
                itembinding.item = i
                itembinding.viewModel = binding.viewModel
                binding.linearLayout.addView(itembinding.root)
            }
        })

        binding.viewModel?.nkey?.observe(this, Observer {
            when (it) {
                //跟进
                "archive.purge", "archive.order", "archive.thenend", "archive.index" -> {
                    binding.genjin.visibility = View.VISIBLE
                }
                //战败审核
                "archivecheck.fail" -> {
                    binding.zhanbai.visibility = View.VISIBLE

                }
                //无效审核
                "archivecheck.invalid" -> {
                    binding.wuxiao.visibility = View.VISIBLE


                }
                //战败管理
                "archivepot.index", "archivepot.discard", "archivepot.failure", "archivepot.invalid" -> {
                    binding.zhanbai.visibility = View.VISIBLE
                }
            }
        })
        binding.viewModel?.phoneMsg?.observe(this, Observer { it ->
            if(it.indexOf("*") !=-1){
                binding.messageBut.visibility = View.GONE
            }
        })
    }

    override fun lazyInit() {
        Log.i("DefeatDetailsFragment","override lazyinit")
    }


}
